# RandomForest
#效果不佳，与真实值相差1.4左右
# Importing the libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

#计算损失，越大越好
def rmse(actual, predict):
    predict = np.array(predict)
    actual = np.array(actual)
    distance = predict - actual
    square_distance = distance ** 2
    mean_square_distance = square_distance.mean()
    score = np.sqrt(mean_square_distance)
    return -score

# Importing the dataset
dataset = pd.read_csv('d_train_20180102.csv',encoding='gb2312').fillna(0)
X = dataset.iloc[:,1:-1].values
y = dataset.iloc[:,-1].values

# Encoding categorical data
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
X[:, 0] = LabelEncoder().fit_transform(X[:, 0])
X[:, 2] = 0
#X.fillna('0')
print('编码完成···')

# Splitting the dataset into the Training set and Test set
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 0)
print('数据集划分完成···')

#定义模型
from sklearn.ensemble import RandomForestRegressor
n_estimators = 50
max_depth = 5
min_samples_split = 4
min_samples_leaf = 2
model = RandomForestRegressor(n_estimators = n_estimators,
                              max_depth = max_depth,
                              min_samples_split = min_samples_split,
                              min_samples_leaf = min_samples_leaf,
                              )

#Fitting
model.fit(X_train,y_train)
print("Traing Score:%f"%model.score(X_train,y_train))
print("Testing Score:%f"%model.score(X_test,y_test))
y_pred = model.predict(X_test)
final_loss = rmse(y_test,y_pred)
print('预测完成···')

importance = model.feature_importances_
importance_data = pd.DataFrame(importance,columns = ["importance"])
importance_data = importance_data.sort_values("importance",ascending = False)
print(importance_data)

for i in range(20,40):
	print('true:',y_test[i],'  pred:',y_pred[i])
print('loss:',final_loss)